Resource = function() {
	return {
		
		init : function() {
			$('#resourceDataGrid').datagrid({
				url				: basePath + '/pc/resource/list.htm',
				fit				: true,
				fitColumns		: true,
				striped			: true,
				rownumbers		: true,
				autoRowHeight	: true,
				singleSelect    : true,
				nowrap			: true,
				pagination		: true,
				pageSize        : 50,
				pageList        : [50, 100, 150, 200, 250],
				method 			: 'POST',
				columns			: [[
				    {field : 'resourceId',      title : 'resourceId',    width : 100,   hidden  : true},
				    {field : 'name',            title : '商户名称',       width : 150,   formatter : function(value, rowData, index) {
				    	if(value.length > 25) {
				    		value = value.substring(0, 25) + '...';
				    	}
		    			return '<a href="javascript:void(0)" onclick="Resource.approve(\'' + rowData.resourceId + '\')">'+value+'</a>';
				    }},
				    {field : 'serviceName',     title : '服务项目名称',    width : 70,   align : 'center'},
				    {field : 'industryName',    title : '所属行业',        width : 75,   align : 'center'},
				    {field : 'dataFlag',        title : '状态',            width : 60,   align: 'center', formatter : function(value, rowData, index){
				    	 var result="";
				    	 if(value == "0"){
				    		 result ="新增";
				    	 } else if(value == "1") {
				    		 result = "更新";
				    	 } else{
				    		 result = "";
				    	 }
				    	 return result;
				    }},
				    {field : 'userName',        title : '添加人',         width : 60,   align : 'center'},
				    {field : 'addDate',         title : '添加时间',       width : 90,   align : 'center'},
				    {field : 'checkFlag',       title : '审核状态',   width : 60,  align : 'center',  formatter : function(value, rowData, index) {
				    	var result = "";
				    	if(value == "0") {
				    		result = "已审核";
				    	} else if(value == "1") {
				    		result = "未审核";
				    	} else {
				    		result = "";
				    	}
				    	return result;
				    }},
				    {field : 'opt',             title : '操作',          width : 60,   align : 'center', formatter : function(value, rowData, index) {
                        var result = "";
				    	/*if(rowData.state == "3") {
				    		//入库完成且未审核时显示审核按钮: checkFlag(审核标志 0-已审核 1-未审核),state(1-入库等待,2-入库中,3-入库完成,4-入库失败)
				    		//中央库删除商户时是根据商户ID进行的,故入库完成之后才能删除
				    		result = '<a href="javascript:void(0)" onclick="Resource.approve(\'' + rowData.resourceId + '\')">审核</a>&#12288;' + 
			    			'<a href="javascript:void(0)" onclick="Resource.remove(\'' + rowData.resourceId + '\')">删除</a>';
                        } else{
                        	result = "";
                        }*/
                        result = '<a href="javascript:void(0)" onclick="Resource.approve(\'' + rowData.resourceId + '\')">审核</a>&#12288;' + 
		    			'<a href="javascript:void(0)" onclick="Resource.remove(\'' + rowData.resourceId + '\')">删除</a>';
				    	return result;
				    }}
				]],
				toolbar : '#toolBar'
			});
		},
		
		approve : function(resourceId) {
			window.location.href = "/pc/resource/approve.htm?resourceId=" + resourceId;
		},
		
		detail : function(resourceId) {
			window.location.href = "/pc/resource/detail.htm?resourceId=" + resourceId;
		},
		
		doSearch : function() {
			var provinceId = $("#province").val();
			var cityId = $("#city").val();
			var channelId = $("#channel").val();
			var checkFlag = $("#checkFlag").val();
			var dataFlag = $("#dataFlag").val();
			var shopName = $.trim($("#shopName").val());
			var startTime = $("#startTime").datebox('getValue');
			var endTime = $("#endTime").datebox('getValue');
			if("" != startTime && "" != endTime) {
				if (startTime > endTime) {
					Ict.info('开始时间不能大于结束时间!');
					return;
				}
			}
			$('#resourceDataGrid').datagrid({
				queryParams : {
					'provinceId' : provinceId,
					'cityId'     : cityId,
					'channelId'  : channelId,
					'checkFlag'  : checkFlag,
					'dataFlag'   : dataFlag,
					'shopName'	 : shopName,
					'startTime'  : startTime,
					'endTime'    : endTime
				},
				toolbar : '#toolBar'
			});
		},
		
		exportResource : function() {
			location = "/exportExcel?exportFileName=shops.xlsx&templateFileName=resources.xlsx";
		},
		
		getCity : function() {
			var provinceId = $("#province").val();
			$.ajax({
				url     : '/pc/cities/getCities.htm',
				type    : 'POST',
				data    : {
					'provinceId' : provinceId
				},
				success : function(data) {
					Resource.fillCityField(data);
				}
			});
		},
		
		/**
		 * 填充城市下拉框.
		 */
		fillCityField : function(data) {
			var cityTd = $("#cityTd");
			cityTd.html(""); //先清空数据
			var allHtml = 
				"<select class='cekxl' name='city' id='city'>" +
				"	<option value=''>请选择...</option>";
			var demo = "<option value='_cityId'>_cityName</option>";
			for(var i = 0; i < data.length; i++) {
				var html = demo;
				html = html.replaceAll("_cityId", data[i].cityid)
					.replaceAll("_cityName", data[i].city);
				allHtml += html;
			}
			allHtml += "</select>";
			cityTd.html(allHtml);
		},
		
		remove : function(resourceId) {
			Ict.confirm("是否删除该商户?", function(yes) {
				if(yes) {
					$.post('/pc/resource/remove.htm', {'resourceId' : resourceId}, function(data) {
						if("SUCCESS" == data) {
							Ict.info("删除成功!");
							$('#resourceDataGrid').datagrid('reload');
						} else {
							Ict.info("删除失败!");
						}
					});
				}
			});
		}
	};
}();

$(function(){
	Resource.init();
	Resource.getCity();
	//字符串替换	
	String.prototype.replaceAll  = function(s1, s2) {  
		return this.replace(new RegExp(s1, "gm"), s2);//这里的gm是固定的，g可能表示global，m可能表示multiple。
	};
});